package com.kz.himalaya.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.databinding.DataBindingUtil;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import com.kz.himalaya.R;
import com.kz.himalaya.application.MyApplication;
import com.kz.himalaya.bean.details.music.MusicEntity;
import com.kz.himalaya.databinding.ActivityMusicPlayerBinding;
import com.kz.himalaya.interfaces.HttpService;
import com.kz.himalaya.service.PlayService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MusicPlayerActivity extends AppCompatActivity implements Callback<MusicEntity>, View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private ActivityMusicPlayerBinding binding;
    private PlayService playService;
    private ServiceConnection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_music_player);
        final long trackId = getIntent().getLongExtra("trackId",-1);
        HttpService service = ((MyApplication) getApplication()).getService();
        if (trackId!=-1) {
            service.getMusicData(trackId).enqueue(this);
        }
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                playService = ((PlayService.MyBinder) service).getService();
                Log.i("1620", "onServiceConnected: "+playService.hashCode());
            }


            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        Intent intent = new Intent(this, PlayService.class);
        bindService(intent, connection, Service.BIND_AUTO_CREATE);
        if (playService==null) {
            startService(intent);
        }
        binding.next.setOnClickListener(this);
        binding.preBtn.setOnClickListener(this);
        binding.playOrPause.setOnClickListener(this);
        binding.seek.setOnSeekBarChangeListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }

    @Override
    public void onResponse(Call<MusicEntity> call, Response<MusicEntity> response) {

        MusicEntity body = response.body();
        if (body!=null) {
            binding.setMusicData(body);
            playService.loadMusic(body.getTrackInfo().getPlayUrl32());
        }
    }

    @Override
    public void onFailure(Call<MusicEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: "+t.getMessage());
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.playOrPause:
                playService.pause();
                break;
            case R.id.preBtn:
//                playService.loadMusic();
                break;
            case R.id.next:
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
//        int progress = seekBar.getProgress();
//        double percent = progress / seekBar.getMax();
//        playService.seekToPorgress(percent);
    }
}
